package team.epos.utils.util;

import team.epos.utils.annotation.PropertiesValue;

import java.lang.reflect.Field;
import java.util.Properties;

/**
 * Create by Vic Xu on 7/15/2018
 *
 * @author Vic Xu
 */
public class PropertiesBeanUtil<T> {


    public static <T> T getBean(Class<T> c, Properties conf) {
        T t = null;
        try {
            t = c.newInstance();
            Field[] fields = c.getDeclaredFields();
            for (Field f : fields) {
                if (f.isAnnotationPresent(PropertiesValue.class)) {
                    f.setAccessible(true);
                    String value = f.getAnnotation(PropertiesValue.class).value();
                    f.set(t, conf.getProperty(value));
                }
            }
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return t;
    }
}
